<?php
//=========================
//Quan Van Sinh
//qvsinh@yahoo.com
//Base Functions
//=========================
//Function checklogin return true or false
function check_log_in() {

    global $db;

    if (!isset($_SESSION['adlogin'])) {
        return false;
    }

    $username = mysql_real_escape_string($_SESSION['adlogin']["username"]);
    $password = mysql_real_escape_string($_SESSION['adlogin']["password"]);

    $result = $db->query_first("SELECT * FROM admin WHERE username='".$username."' AND password='".$password."' AND active='yes'");

    if ($result) {

        $_SESSION['adlogin']["id"] 				= intval($result["id"]);
        $_SESSION['adlogin']["username"] 		= $result["username"];
        $_SESSION['adlogin']["password"] 		= $result["password"];
        $_SESSION['adlogin']["permiss"] 		= $result["permiss"];
        $_SESSION['adlogin']["email"] 			= $result["email"];
		
        return true;

    } else {
        return false;
    }


}

function check_login_single(){
	global $db;
	$username = mysql_real_escape_string($_SESSION['adlogin']["username"]);
    $password = mysql_real_escape_string($_SESSION['adlogin']["password"]);
    $result = $db->query_first("SELECT last_login FROM admin WHERE username='".$username."' AND password='".$password."' AND active='yes'");
	if ($result["last_login"]==$_SESSION['adlogin']["last_login"]) {
        return true;
    } else {
		$_SESSION['already_login'] = 1;
        return false;
    }
}

//FUNCTION CHECK PERMISSION
function check_permission($user_id,$module) {

    global $db,$CONFIG;

    $permiss	 = $db->query_first("SELECT permiss FROM admin WHERE id=".intval($user_id));
    $arr_permiss  = explode(',',$permiss["permiss"]);

    if(in_array($module,$arr_permiss)||empty($permiss["permiss"]) || in_array($module, $CONFIG['array_modules_list_allow'])) {
        return true;
    }

    return false;

}
function show_error() {

    global $CONFIG;

    if(!empty($CONFIG['error_message'])) {

        echo '<div class="form_error">
						<ul>
							<li class="class1"><b>Thông báo lỗi:</b></li>
							'.$CONFIG['error_message'].'
						</ul>
			</div>';
    }
}

function show_success() {

    global $CONFIG;

    if(!empty($CONFIG['success_message'])) {

        echo '<div class="form_ok">
					<b>'.$CONFIG['success_message'].'</b>
			  </div>';
    }
}

//Funtion init session $param(string) = "session_name1,session_value1,session_name2,session_value2"...
function init_session($param) {

    $arr = explode(',',$param);

    if(count($arr)>1) {

        for($i=0;$i<count($arr);$i+=2) {

            $_SESSION[$arr[$i]] = $arr[$i+1];

        }
    }

}

//Function unset session $param(string) = "session_name1,session_name2,"...
function unset_session($param) {

    $arr = explode(',',$param);

    if(count($arr)>1) {

        foreach($arr as $i=>$rows) {

            unset($_SESSION[$rows]);

        }
    }

}

//FUNCTION PAGE
function paging($page,$total,$url) {
    if ($total>1) {
        if ($page!=1) {
            $dau=$page-5;
            if ($dau<=0) {$dau=1;$dit=5-$page;}
            $dit=$dit+$page+5;
            if ($dit>$total) {
                $dit=$total;$dau=$total-10;
                if ($dau<=0) {$dau=1;}
            }
        }
        else {
            $dau=1;$dit=10;
            if ($dit>$total) {$dit=$total;}
        }
        if ($page >$total) {$dau=1;$dit=1;}
        for ($i=$dau;$i<=$dit;$i++) {
            if ($i==$page) {

                $pages.="<li><a class='currentpage'>$i</a></li>";

            }
            else {

                $pages.="<li><a href=".$url."&page=$i>$i</a></li>";

            }
        }
        // get page >> <<
        //if($page <=1){$backpage = 1;}else{$backpage = $page - 1;}
        //if($page >=$total){$nextpage = $total;}else{$nextpage = $page + 1;}
        // page string

        echo '<div class="page_un">
			   <div class="pagination">
				<ul>';

        echo "<li><a href=".$url."&page=1 class='prevnext'>«</a></li>";
        echo $pages ;
        echo"<li><a href=".$url."&page=".$total." class='prevnext'>»</a></li>";
        echo'</ul>
			  </div>
		     </div>';

    //return $page_str;

    } else {
        return false;
    }
}

//==========================================
//Quan Van Sinh
//sinh@vietnambiz
//===========================================
//tra ve trang thai check box form
function setSelectedBoxValue($postValue,$dbValue) {
    if($postValue==$dbValue) {
        return 'selected="selected"';
    }
}

//tra ve trang thai select box form
function setCheckedBoxValue($postValue,$dbValue,$default=false) {
    if($postValue==$dbValue) {
        return 'checked="checked"';
    }else if($default) {
            return 'checked="checked"';
        }
}
//Lay chuoi duong dan trang
function get_string_url($arrRemoveParam=array('page')) {
    $strParamsGet = '?';
    $i=0;
    in_array($key, $arrRemoveParam);
    foreach ($_GET as $key=>$item) {
        if($i==0 && !in_array($key, $arrRemoveParam)) {
            $strParamsGet .= $key.'='.$item;
        }else if($i!=0 && !in_array($key, $arrRemoveParam)) {
                $strParamsGet .= '&'.$key.'='.$item;
            }
        $i++;
    }
    return $_SERVER['PHP_SELF'].$strParamsGet;
}
//=============================================================

function print_gzipped_page() {

    global $HTTP_ACCEPT_ENCODING;
    if( headers_sent() ){
        $encoding = false;
    }elseif( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ){
        $encoding = 'x-gzip';
    }elseif( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ){
        $encoding = 'gzip';
    }else{
        $encoding = false;
    }

    if( $encoding ){
        $contents = ob_get_contents();
        ob_end_clean();
        header('Content-Encoding: '.$encoding);
        print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
        $size = strlen($contents);
        $contents = gzcompress($contents, 9);
        $contents = substr($contents, 0, $size);
        print($contents);
        exit();
    }else{
        ob_end_flush();
        exit();
    }
}
//cat chuoi theo tu
function cutString($str, $len) {
	$arrWord = explode(' ', $str);
    if(count($arrWord)>$length+1) {
        return implode(' ', array_slice($arrWord, 0, $length)).' ...';
    }else {
        return implode(' ', array_slice($arrWord, 0, $length));
    }
}
function limitWord($str, $length) {
    $arrWord = explode(' ', $str);
    if(count($arrWord)>$length+1){
        return implode(' ', array_slice($arrWord, 0, $length)).' ...';
    }else{
        return implode(' ', array_slice($arrWord, 0, $length));
    }
}
function showStatus($status)
{
    switch($status)
    {
           case 'actived': return '<font style="font-weight: bold" color="black">Đang giao dịch</font>'; break;
           case 'pending': return '<font style="font-weight: bold" color="red">Chờ kích hoạt</font>';break;
           case 'draft': return '<font style="font-weight: bold" color="#ccc">Tin nháp</font>';break;
           case 'closed': return '<font style="font-weight: bold" color="gray">Hết hàng</font>';break;
    }
}
?>